package com.lrz.service.impl;

import com.lrz.common.IdWorker;
import com.lrz.dao.UserMapper;
import com.lrz.pojo.*;
import com.lrz.dao.CommentRepository;
import com.lrz.pojo.dto.CommentDTO;
import com.lrz.service.CommentService;
import com.lrz.service.LikeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService{

    @Resource
    private CommentRepository commentRepository;

    @Resource
    private IdWorker idWorker;

    @Resource
    private UserMapper userMapper;

    @Resource
    private LikeService likeService;

    @Override
    public List<Comment> findAll() {
        return commentRepository.findAll();
    }

    @Override
    public void add(Comment comment) {
        comment.set_id(idWorker.nextId()+"");
        comment.setPublishDate(LocalDateTime.now());
        comment.setThumbup(0);
        commentRepository.insert(comment);
    }

    @Override
    public List<CommentDTO> findByArticleId(String articleId, Long userId) {
        List<CommentDTO> list = new ArrayList<>();
        List<Comment> list1 = commentRepository.findByArticleIdAndParentIdOrderByPublishDateDesc(articleId,"0");
        for (Comment comment : list1) {
            CommentDTO commentDTO1 = new CommentDTO();
            tranCommentDTO(commentDTO1, comment);
            commentDTO1.setLike(likeService.isLike(comment.get_id(), userId));
            List<CommentDTO> listSon = new ArrayList<>();
            List<Comment> sonComments = commentRepository.findByParentIdOrderByPublishDateAsc(comment.get_id());
            for (Comment sonComment : sonComments) {
                CommentDTO commentDTO2 = new CommentDTO();
                tranCommentDTO(commentDTO2, sonComment);
                commentDTO2.setLike( likeService.isLike(sonComment.get_id(), userId));
                listSon.add(commentDTO2);
            }
            commentDTO1.setSons(listSon);
            list.add(commentDTO1);
        }
        return list;
    }

    @Override
    public void addThumbup(String id) {
        Comment comment = commentRepository.findBy_id(id);
        comment.setThumbup(comment.getThumbup() + 1);
        commentRepository.save(comment);
    }

    @Override
    public void decThumbup(String id) {
        Comment comment = commentRepository.findBy_id(id);
        comment.setThumbup(comment.getThumbup() - 1);
        commentRepository.save(comment);
    }

    private void tranCommentDTO(CommentDTO commentDTO, Comment comment) {
        commentDTO.set_id(comment.get_id());
        commentDTO.setArticleId(comment.getArticleId());
        commentDTO.setContent(comment.getContent());
        User user = userMapper.selectById(comment.getUserId());
        user.setPassword("");
        commentDTO.setUser(user);
        User user1 = userMapper.selectById(comment.getAcceptId());
        user1.setPassword("");
        commentDTO.setAcceptUser(user1);
        commentDTO.setParentId(comment.getParentId());
        commentDTO.setPublishDate(comment.getPublishDate());
        commentDTO.setThumbup(comment.getThumbup());
    }


}
